package com.ormbench.base.action;

import java.util.Map;

import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;

import com.ormbench.base.model.OrmBench;


@UrlBinding("/action/ormbench")
public class OrmBenchAction extends BaseAction {
	private static final String RESOLUTION_ORMBENCH = "/ftl/ormbench.ftl";
	
	@ValidateNestedProperties({
		@Validate(field="scale", required=true, minvalue=1, maxvalue=5, on="run"),
		@Validate(field="jdbcDriver", required=true, on="run"),
		@Validate(field="jdbcUrl", required=true, on="run"),
		@Validate(field="jdbcUsername", required=true, on="run"),
		@Validate(field="jdbcPassword")
	})
	private OrmBench ormBench;
	public OrmBench getOrmBench() { return ormBench; }
	public void setOrmBench(OrmBench ormBench) { this.ormBench = ormBench; }

	private Map<String, Long> ebeanResults;
	public Map<String, Long> getEbeanResults() { return ebeanResults; }
	public void setEbeanResults(Map<String, Long> ebeanResults) { this.ebeanResults = ebeanResults; }
	
	private Map<String, Long> hibernateResults;
	public Map<String, Long> getHibernateResults() { return hibernateResults; }
	public void setHibernateResults(Map<String, Long> hibernateResults) { this.hibernateResults = hibernateResults; }
	
	
	@Before(stages=LifecycleStage.BindingAndValidation)
	public void populate() {
		ormBench = new OrmBench();
	}
	
	@DefaultHandler
	public Resolution read() {
		return new ForwardResolution(RESOLUTION_ORMBENCH);
	}
	
	public Resolution run() throws Exception {
		OrmBench ormBench = this.getOrmBench();
		
		ebeanResults = ormBench.run(OrmBench.ORM_EBEAN);
		hibernateResults = ormBench.run(OrmBench.ORM_HIBERNATE);
		
		return new ForwardResolution(RESOLUTION_ORMBENCH);
	}
}
